﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using iOak.Domain.Resumes;
using iOak.Business.Resumes;
using iOak.Domain.Campaigns;
using iOak.Common;
using iOak.Business.Campaigns;

namespace iOak.Web.Administration
{
    public partial class PolicyDetails : BasePage
    {
        private const string PolicySessionName = "PolicyDetails_PolicyItem";
        private WorkAssistant _workassistant = new WorkAssistant();

        private Policy PolicyItem
        {
            get
            {
                Policy Policy = null;
                if (Session[PolicySessionName] != null)
                {
                    Policy = Session[PolicySessionName] as Policy;
                }
                return Policy;
            }
            set
            {
                Session[PolicySessionName] = value;
            }
        }

        /// <summary>
        /// 页面模式0=新建;1=编辑;
        /// </summary>
        private int PageMode
        {
            get
            {
                int pageMode = 0;
                if (this.ViewState["PageMode"] != null)
                {
                    pageMode = int.Parse(this.ViewState["PageMode"].ToString());
                }
                return pageMode;
            }
            set
            {
                this.ViewState["PageMode"] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack) 
            {
                IniResumeTemplateList();
                txtPublishedOn.Attributes.Add("Readonly", "Readonly");
                if (Request.QueryString["PolicyId"] != null)
                {
                    PolicyService PolicyService = new PolicyService();
                    Guid PolicyId = Guid.Parse(Request.QueryString["PolicyId"]);
                    PolicyItem = PolicyService.Retrieve(PolicyId);
                    PageMode = 1;
                    IniData();
                }
                else
                {
                    //txtPublishedOn.Text = DateTime.Now.ToString("yyyy-MM-dd");
                    var user = _workassistant.CurrentUser;
                    PolicyItem = new Policy();
                    PolicyItem.Policyid = Guid.NewGuid();
                    PolicyItem.ModifiedBy = user.UserId; //_workassistant.CurrentUser.UserId;;
                    PolicyItem.ModifiedOn = DateTime.Now;
                    PolicyItem.CreatedBy = user.UserId; //_workassistant.CurrentUser.UserId;;
                    PolicyItem.CreatedOn = DateTime.Now;
                    PageMode = 0;
                }
            }
        }

        protected void btnOk_Click(object sender, EventArgs e)
        {
            PolicyItem.Subject = txtSubject.Text.Trim();
            PolicyItem.Description = txtDescription.Text.Trim();
            PolicyItem.PublishedOn = txtPublishedOn.Text.Trim();
            PolicyItem.PublishedBy = txtPublishedBy.Text.Trim();
            PolicyItem.ResumeTemplateId = Guid.Parse(ddlResumeTemplate.SelectedValue);
            PolicyService PolicyService = new PolicyService();
            if (PageMode == 0)
            {
                PolicyService.Create(PolicyItem);
            }
            else if (PageMode == 1)
            {
                PolicyService.Update(PolicyItem);
            }
            Response.Redirect("/Administration/PolicyList.aspx");
        }

        private void IniResumeTemplateList()
        {
            ResumeTemplateService resumeTemplateService = new ResumeTemplateService();
            List<ResumeTemplate> resumeTemplateList = resumeTemplateService.LoadAll();
            ddlResumeTemplate.DataSource = resumeTemplateList;
            ddlResumeTemplate.DataTextField = "Title";
            ddlResumeTemplate.DataValueField = "ResumeTemplateId";
            ddlResumeTemplate.DataBind();
        }

        private void IniData()
        {
            txtSubject.Text = PolicyItem.Subject;
            txtDescription.Text = PolicyItem.Description;
            txtPublishedOn.Text = PolicyItem.PublishedOn;
            txtPublishedBy.Text = PolicyItem.PublishedBy;
            ddlResumeTemplate.SelectedValue = PolicyItem.ResumeTemplateId.ToString();
        }
    }
}